package com.ruoyi.project.module.application.service;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.module.applicationProperty.domain.ApplicationProperty;
import com.ruoyi.project.module.applicationProperty.service.IApplicationPropertyService;
import com.ruoyi.project.module.releaseGroup.service.IReleaseGroupService;
import com.ruoyi.project.module.releaseProperty.service.IReleasePropertyService;
import com.ruoyi.project.module.task.mapper.TaskUtilsMapper;
import org.shoukaiseki.koori.model.ResultKoori;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.module.application.mapper.ApplicationMapper;
import com.ruoyi.project.module.application.domain.Application;
import com.ruoyi.project.module.application.service.IApplicationService;
import com.ruoyi.common.support.Convert;

import javax.annotation.Resource;

/**
 * 配置应用管理 服务层实现
 * 
 * @author ruoyi
 * @date 2018-11-06
 */
@Service
public class ApplicationServiceImpl implements IApplicationService 
{

	@Resource
	private TaskUtilsMapper taskUtilsMapper;

	@Resource
	private ApplicationMapper applicationMapper;

	@Resource
	private IApplicationPropertyService applicationPropertyService;

	@Resource
	private IReleaseGroupService releaseGroupService;

	/**
     * 查询配置应用管理信息
     * 
     * @param configApplicationId 配置应用管理ID
     * @return 配置应用管理信息
     */
    @Override
	public Application selectApplicationById(Long configApplicationId)
	{
	    return applicationMapper.selectApplicationById(configApplicationId);
	}
	
	/**
     * 查询配置应用管理列表
     * 
     * @param application 配置应用管理信息
     * @return 配置应用管理集合
     */
	@Override
	public List<Application> selectApplicationList(Application application)
	{
	    return applicationMapper.selectApplicationList(application);
	}
	
    /**
     * 新增配置应用管理
     * 
     * @param application 配置应用管理信息
     * @return 结果
     */
	@Override
	public int insertApplication(Application application)
	{
		Date currentDate = taskUtilsMapper.getCurrentDate();
		String userName = ShiroUtils.getUser().getLoginName();
		application.setCreateBy(userName);
		application.setCreateTime(currentDate);
		application.setUpdateBy(userName);
		application.setUpdateTime(currentDate);
	    return applicationMapper.insertApplication(application);
	}
	
	/**
     * 修改配置应用管理
     * 
     * @param application 配置应用管理信息
     * @return 结果
     */
	@Override
	public int updateApplication(Application application)
	{
		Date currentDate = taskUtilsMapper.getCurrentDate();
		String userName = ShiroUtils.getUser().getLoginName();
		application.setCreateBy(null);
		application.setCreateTime(null);
		application.setUpdateBy(userName);
		application.setUpdateTime(currentDate);
	    return applicationMapper.updateApplication(application);
	}

	/**
     * 删除配置应用管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteApplicationByIds(String ids)
	{
		return applicationMapper.deleteApplicationByIds(Convert.toStrArray(ids));
	}

	@Override
	public boolean release(String idsStr) {
		Long[] ids = Convert.toLongArray(idsStr);
		for (Long applicationId:ids) {
			release(applicationId);
		}
		return true;
	}

	public void release(Long applicationId){
		Application application = selectApplicationById(applicationId);
		ApplicationProperty applicationPropertyFind = new ApplicationProperty();
		applicationPropertyFind.setConfigApplicationId(applicationId);
		List<ApplicationProperty> applicationProperties = applicationPropertyService.selectApplicationPropertyList(applicationPropertyFind);
		application.setConfigApplicationPropertyList(applicationProperties);
		ResultKoori release = releaseGroupService.release(application);

		Application temp=new Application();
		temp.setReleaseVersion(release.getCurrentVersion());
		temp.setConfigApplicationId(applicationId);
		temp.setModify(0);
		updateApplication(temp);

	}


}
